package com.corner.utils;

import android.content.Context;

import com.corner.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * ============================================================
 * <p/>
 * 版权 ：逗同学
 * <p/>
 * 作者：韩炜伟
 * <p/>
 * 版本 ：1.0
 * <p/>
 * 创建日期 ： 2015年1月31日下午1:40:00
 * <p/>
 * 描述 ：时间格式化等工具
 * <p/>
 * <p/>
 * 修订历史 ：
 * <p/>
 * ============================================================
 */
public class DateFormatUtil {

    public static final int SECONDS_OF_ONE_DAY = 3600 * 24;
    public static final int SECONDS_OF_ONE_HOUR = 3600;
    public static final int SECONDS_OF_ONE_MINUTE = 60;

    /**
     * 获取当前时间戳
     *
     * @return
     */
    public static long getCurrentTimeInMills() {
        return System.currentTimeMillis();
    }

    public static String getCNTime() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMddHHmmss");
        return dateFormat.format(new Date());
    }

    public static String getTime(String patter, long time) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(patter);
        return dateFormat.format(new Date(time));
    }

    public static String getDay() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMdd");
        return dateFormat.format(new Date());
    }

    public static int getDayOfYear(long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(time));
        return calendar.get(Calendar.DAY_OF_YEAR);
    }

    public static int getDayOfYear() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.DAY_OF_YEAR);
    }

    public static String getShowTime(long createTime, Context context) {
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yy-M-d HH:mm");
//        return dateFormat.format(createTime);
        String result = "";
        long tiemoff = (DateFormatUtil.getCurrentTimeInMills() - createTime) / 1000;
        int publishDay = DateFormatUtil.getDayOfYear(createTime);
        int currentDay = DateFormatUtil.getDayOfYear();
        if (publishDay == currentDay) {
            if (tiemoff < SECONDS_OF_ONE_MINUTE) {
                result = context.getString(R.string.just_now);
            } else if (tiemoff < SECONDS_OF_ONE_HOUR) {
                result = tiemoff / SECONDS_OF_ONE_MINUTE + context.getString(R.string.mins_before);
            } else if (tiemoff < SECONDS_OF_ONE_DAY) {
                result = tiemoff / SECONDS_OF_ONE_HOUR
                        + context.getString(R.string.hours_before);
            }
        } else if (currentDay - publishDay < 7) {
            result = currentDay - publishDay + "天前";
        } else if (currentDay - publishDay < 14) {
            result = (currentDay - publishDay) / 7 + "周前";
        } else {
            result = DateFormatUtil.getTime("yy-M-d HH:MM", createTime);
        }
        return result;
    }

    public static String getShowTimeWithHmInTodayOrMdBeforeToday(long time, Context context) {
        String result = "";
                int publishDay = DateFormatUtil.getDayOfYear(time);
        int currentDay = DateFormatUtil.getDayOfYear();
        if (publishDay == currentDay) {
            result = DateFormatUtil.getTime("HH:mm",time);
        } else{
            result = DateFormatUtil.getTime("yy-M-d",time);
        }
        return result;
    }
//        String result = "";
//        long tiemoff = (DateFormatUtil.getCurrentTimeInMills() - createTime)/1000;
//        int publishDay = DateFormatUtil.getDayOfYear(createTime);
//        int currentDay = DateFormatUtil.getDayOfYear();
//        if (publishDay == currentDay) {

//            if (tiemoff < 3600) {
//                result = tiemoff / 60 + context.getString(R.string.mins_before);
//            } else if (tiemoff < 3600 * 6) {
//                result = tiemoff / 3600
//                        + context.getString(R.string.hours_before);
//            } else {
//                result = context.getString(R.string.today)
//                        + DateFormatUtil.getTime("HH:mm", createTime);
//            }
//        } else if (currentDay - publishDay == 1) {
//            result = context.getString(R.string.ysestoday)
//                    + DateFormatUtil.getTime("HH:mm", createTime);
//        } else {
//            result = DateFormatUtil.getTime("M-d", createTime);
//        }
//        return result;
//    }
}
